  icol:= IWCGJQGrid.JQGridOptions.ColModel.ItemsByName['ProductCode'];
  if Assigned(icol) then
  begin
    icol.SummaryType:= gcstCount;
    icol.SummaryTpl:= '{0}';
  end;

  icol:= IWCGJQGrid.JQGridOptions.ColModel.ItemsByName['UnitPrice'];
  if Assigned(icol) then
  begin
    icol.SummaryType:= gcstAvg;
    icol.SummaryTpl:= 'Average: {0}';
  end;

  icol:= IWCGJQGrid.JQGridOptions.ColModel.ItemsByName['Stock'];
  if Assigned(icol) then
  begin
    icol.SummaryType:= gcstSum;
    icol.SummaryTpl:= 'Sum: {0}';
  end;

  IWCGJQGrid.JQGridOptions.Grouping:= True;
  iGroup:= IWCGJQGrid.JQGridOptions.GroupingView.Groups.Add;
  if Assigned(iGroup) then
  begin
    iGroup.GroupField:= 'PurchaseDate';
    iGroup.GroupText:= '<b>{0} - {1} Item(s)</b>';
    iGroup.GroupOrder:= gsoDesc;
    iGroup.GroupSummary:= True;
    iGroup.GroupColumnShow:= True;
  end;
  IWCGJQGrid.JQGridOptions.GroupingView.GroupCollapse:= False;

  IWCGJQGrid.JQGridOptions.FooterRow:= True;
  IWCGJQGrid.JQGridOptions.UserDataOnFooter:= True;
  IWCGJQGridDataSetProvider.OnGetColSummaries:= GridDataSetProviderGetColSummaries;

  
procedure TFrameGrid.GridDataSetProviderGetColSummaries(Sender: TObject; out ANames: TIWCGJQStrArray;
    out AValues: TIWCGJQVarArray);
var
  Select: string;
  i,l: Integer;
  SummaryOp: string;
  Col: TIWCGJQGridCol;
  Query: TADOQuery;
begin
  Select:= '';
  for i:= 0 to IWCGJQGrid.JQGridOptions.ColModel.Count-1 do
  begin
    Col:= IWCGJQGrid.JQGridOptions.ColModel[i];
    if (Col.Name <> '') and (Col.SummaryType <> gcstNone) then
    begin
      case Col.SummaryType of
        gcstSum: SummaryOp:= 'SUM';
        gcstAvg: SummaryOp:= 'AVG';
        gcstCount: SummaryOp:= 'COUNT';
        gcstMin: SummaryOp:= 'MIN';
        gcstMax: SummaryOp:= 'MAX';
        else Continue;
      end;

      l:= Length(ANames);
      SetLength(ANames,l+1);
      ANames[l]:= Col.Name;

      if Select <> '' then
        Select:= Select + ', ';
      Select:= Select + SummaryOp + '(' + Col.Name + ')';
    end;
  end;
  if Select = '' then Exit;

  Query:= TADOQuery.Create(nil);
  try
    Query.Connection:= UserSession.SqlServer;
    Query.SQL.Assign(qryProducts.SQL);
    Query.SQL[0]:= 'select ' + Select + ' from Products';
    Query.SQL[2]:= '';
    Query.Open;
    if Query.Eof then Exit;
    for i:= 0 to Query.FieldCount-1 do
    begin
      l:= Length(AValues);
      SetLength(AValues,l+1);
      AValues[i]:= Query.Fields[i].Value;
    end;
  finally
    Query.Free;
  end;
end;
  